package com.example.myapplication;

import static com.example.myapplication.MovieDatabase.MIGRATION_1_2;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
import androidx.room.Room;

import com.bumptech.glide.Glide;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private LinearLayout movieContainer;

    public static Map<String, List<String>> movieComments = new HashMap<>(); // 存储每部电影的评论

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        movieContainer = findViewById(R.id.movieContainer);

        // 初始化数据库
        MovieDatabase db = Room.databaseBuilder(this, MovieDatabase.class, "movie-db")
                .allowMainThreadQueries() // 在主线程中进行数据库操作，实际项目中应该使用线程或协程
                .addMigrations(MIGRATION_1_2) // 添加 Migration 对象
                .build();

        MovieDao movieDao = db.movieDao();

        // 从数据库加载电影数据
        List<Movie> movies = movieDao.getAllMovies();

        // 如果数据库为空，则添加一些初始数据
        if (movies.isEmpty()) {
            MovieDataInitializer.initializeMovieData(movieDao); // 使用子线程插入数据
        }

        // 创建电影卡片
        createMovieCards(movies);
    }

    // 创建电影卡片
    private void createMovieCards(List<Movie> movies) {
        for (Movie movie : movies) {
            View movieCard = getLayoutInflater().inflate(R.layout.movie_card, movieContainer, false);

            ImageView poster = movieCard.findViewById(R.id.poster);
            TextView name = movieCard.findViewById(R.id.name);
            Button commentButton = movieCard.findViewById(R.id.commentButton);

            // 使用 Glide 加载海报图片
            Glide.with(this)
                    .load(movie.getPosterPath())
                    .into(poster);
            name.setText(movie.getName());

            // 设置评论按钮点击事件
            commentButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 跳转到评论界面
                    Intent intent = new Intent(MainActivity.this, CommentActivity.class);
                    intent.putExtra("movieName", movie.getName()); // 传递电影名称
                    startActivity(intent);
                }
            });

            movieContainer.addView(movieCard);
        }
    }

    // 数据库模型 (Movie)
    @Entity
    public static class Movie {
        @PrimaryKey(autoGenerate = true)
        public int id;
        @ColumnInfo(name = "name")
        public String name;
        @ColumnInfo(name = "poster_path")
        public int posterPath; // 海报路径 (现在是 drawable 资源 ID)

        // 添加一个空的构造函数
        public Movie() {
        }

        // 添加一个参数匹配的构造函数
        public Movie(String name, int posterPath) {
            this.name = name;
            this.posterPath = posterPath;
        }

        // getter 和 setter
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getPosterPath() {
            return posterPath;
        }

        public void setPosterPath(int posterPath) {
            this.posterPath = posterPath;
        }
    }
}